const express = require("express");
const comment = require("../service/commentService");
const formatResponse = require("../utils/responseFormat");
const commentRouter = express.Router();

//添加评论
commentRouter.post("/", async (req, res, next) => {
  const info = req.body;
  const data = await comment.addComment(info);
  res.send(formatResponse(0, "", data));
});

//删除评论
commentRouter.delete("/:id", async (req, res, next) => {
  const id = req.params.id;
  const data = await comment.deleteComment(id);
  res.send(formatResponse(0, "", data));
});

//分页获取评论
commentRouter.get("/", async (req, res, next) => {
  const page = +req.query.page || 1;
  const limit = +req.query.limit || 10;
  const blogId = +req.query.blogId || -1;
  const keyword = req.query.keyword || "";
  const data = await comment.getCommentList(page, limit, keyword, blogId);
  res.send(formatResponse(0, "", data));
});

module.exports = commentRouter;
